﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.Basic.XML
 * 
 * Description: Class provides static methods to access nodes in an XML Document
 * 
 * Created:     06.05.2012
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2012 by WMSSoft.net
 * 
 * License:     GPLv3
 * 
 * History:     06.05.2012 - matthias    -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WMSSoft.Lib.Basic.Xml
{
    /// <summary>
    /// Provides helper methods used to access XML - documents
    /// </summary>
    public static class cXmlHelpers
    {
        /// <summary>
        /// Gets a reference to Node named in Parameter NodeName
        /// </summary>
        /// <param name="XMLDoc">a reference to XML Document which contains Node in NodeName</param>
        /// <param name="NodeName">Name of Node to reference</param>
        /// <returns><see cref="XMLNode">Node</see> named in Parameter NodeName otherwise null if node does not exists or on exception</returns>
        public static XmlNode GetNode(XmlDocument XMLDoc, string NodeName)
        {
            try
            {
                if (XMLDoc != null)
                {
                    //First, gets the XMLDocuments Root node
                    XmlNode myRootNode = GetRootNode(XMLDoc);
                    //Search XMLNode Named in parameter NodeName
                    if (myRootNode != null)
                    {
                        XmlNode myMatchNode = GetElementNode(myRootNode, NodeName);

                        return myMatchNode;
                    }
                    else
                    {
                        return null;
                    }
                }
                else return null;
            }
            catch (Exception ex)
            {
                //error while processing XML nodes
                CMessagelogging.AppendMessage("Can't read XML - Node '" + NodeName + "'!", ex, EMessageTypes.Error);
                return null;
            }
        }

        /// <summary>
        /// Read XML Node Value
        /// </summary>
        /// <param name="XMLDoc">a reference to XML Document which contains referenced Node and Element</param>
        /// <param name="Element">Values Parent Node</param>
        /// <param name="Value">Value Node</param>
        /// <returns>Text from given Node</returns>
        public static String GetNodeValue(XmlDocument XMLDoc, string Element, string Value)
        {
            try
            {
                if (XMLDoc != null)
                {
                    //Get reference to parent from searched
                    XmlNode myElementNode = GetNode(XMLDoc, Element);

                    if (myElementNode != null)
                    {
                        //read the inner text from values node
                        return GetElementValue(myElementNode, Value);
                    }
                    else return null;
                }
                else return null;
            }
            catch (Exception ex)
            {
                //Error while processing XML nodes
                CMessagelogging.AppendMessage("Can't read Value '" +  Value + "' from XML Node '" + Element + "'!", ex, EMessageTypes.Error);
                return null;
            }
        }

        /// <summary>
        /// Reads the Value from Named XML Node 'Value' in 'ElementNode'
        /// </summary>
        /// <param name="ElementNode">Node - Element that  contains the Value</param>
        /// <param name="Value">Named Value to read inner Text from</param>
        /// <returns>Value - Nodes Inner Text</returns>
        public static string GetElementValue(XmlNode ElementNode, string Value)
        {
            try
            {
                if (ElementNode != null && ElementNode.ChildNodes.Count > 0)
                {
                    //check each child Node
                    foreach (XmlNode Item in ElementNode.ChildNodes)
                    {
                        if (Item.Name == Value)
                        {
                            //If child Nodes Name is equal with value name return the inner text
                            return Item.InnerText;
                        }
                    }

                    return "";
                }
                else return "";
            }
            catch (Exception ex)
            {
                //Error while processing XML Nodes
                CMessagelogging.AppendMessage("Can't read Value Node '" + Value + "' from Parent Node '" + ElementNode.Name + "'!", ex, EMessageTypes.Error);
                return null;
            }
        }
        
        ///// <summary>
        ///// Liest einen verschlüsselten Wert aus dem angegebenen Knoten
        ///// </summary>
        ///// <param name="ElementNode">Der Knoten aus dem der Wert gelesen werden soll</param>
        ///// <param name="Value">Der Name des gesuchten Wertes</param>
        ///// <returns>Der Inhalt des gesuchten Wertes als <see cref="CConfigItem">CConfigItem</see> - Struktur</returns>
        //public static CConfigItem GetElementValueEncrypted(XmlNode ElementNode, string Value) {
        //    try
        //    {
        //        bool ValueEncrypted = false;
        		
        //        if (ElementNode != null && ElementNode.ChildNodes.Count > 0)
        //        {
        //            //alle untergeordneten Werte durchgehen und mit dem gesuchten vergleichen
        //            foreach (XmlNode Item in ElementNode.ChildNodes)
        //            {
        //                if (Item.Name == Value)
        //                {
        //                    //Den Text innerhalb des Wertes zurückgeben
        //                    if (Item.Attributes[CConfigElements.getNodeElementAttributeName(EConfigNodeElementAttributes.ConfItemEncrypted)] != null) {
        //                        //
        //                        ValueEncrypted = bool.Parse(Item.Attributes[CConfigElements.getNodeElementAttributeName(EConfigNodeElementAttributes.ConfItemEncrypted)].Value);
        //                        //return Item.InnerText; 
        //                    } 
                            
        //                    return new CConfigItem(Item.InnerText, ValueEncrypted);
                            
        //                }
        //            }

        //            return null;
        //        }
        //        else return null;
        //    }
        //    catch (Exception ex)
        //    {
        //        //Während der Verarbeitung ist ein Fehler aufgetreten
        //        CMessagelogging.AppendMessage("Der Wert des XML - Elementes '" + Value + "' im Knoten '" + ElementNode.Name + "' konnte nicht ermittelt werden!", ex, EMessageTypes.Error);
        //        return null;
        //    }
        //}

        /// <summary>
        /// Reference the Root Node from XML Document
        /// </summary>
        /// <param name="XMLDoc">a reference to XML Document</param>
        /// <returns>reference to the first Node Element in XML Document</returns>
        public static XmlNode GetRootNode(XmlDocument XMLDoc)
        {
            try
            {
                if (XMLDoc != null && XMLDoc.ChildNodes.Count > 0)
                {
                    foreach (XmlNode Item in XMLDoc.ChildNodes)
                    {
                        if (Item.NodeType == XmlNodeType.Element)
                        {                            
                            return Item;
                        }
                    }
                    return null;
                }
                else return null;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage("Can't read Root Node!", ex, EMessageTypes.Error);
                return null;
            }
        }

        /// <summary>
        /// Reference a Child Node from given Node
        /// </summary>
        /// <param name="ParentNode">Node that contains searched Child Node</param>
        /// <param name="Element">Child Node</param>
        /// <returns>reference to the Child Node</returns>
        public static XmlNode GetElementNode(XmlNode ParentNode, string Element)
        {
            try
            {
                if (ParentNode != null && ParentNode.ChildNodes.Count > 0)
                {
                    //Process all Child Nodes
                    foreach (XmlNode Item in ParentNode.ChildNodes)
                    {
                        //Current Child Node and given Element are equal?
                        if (Item.Name == Element)
                        {
                            //return this node
                            return Item;
                        }
                    }
                    return null;
                }
                else return null;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage("Can't read Element'" + Element + "' from Node '" + ParentNode + "'!", ex, EMessageTypes.Error);
                return null;
            }
        }


    }
}
